<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Terms and their data types
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial012.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial014.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc21">3.1</A>&nbsp;&nbsp;Terms and their data types</H2><UL>
<LI><A HREF="tutorial013.html#toc10">Numbers</A>
<LI><A HREF="tutorial013.html#toc11">Strings</A>
<LI><A HREF="tutorial013.html#toc12">Atoms</A>
<LI><A HREF="tutorial013.html#toc13">Lists</A>
<LI><A HREF="tutorial013.html#toc14">Structures</A>
</UL>

<A NAME="@default13"></A> <A NAME="@default14"></A>
Prolog data (<B>terms</B>) and programs are built from a small set of
simple data-types. In this section, we introduce these data types
together with their syntax (their textual representations). For the
full syntax see the User Manual appendix on Syntax.<BR>
<BR>
<A NAME="toc10"></A>
<H3 CLASS="subsection"><A NAME="htoc22">3.1.1</A>&nbsp;&nbsp;Numbers</H3>
<A NAME="@default15"></A> <A NAME="@default16"></A>
<A NAME="@default17"></A>
Numbers come in several flavours. The ones that are familiar from
other programming languages are integers and floating point numbers.
Integers in ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> can be as large as fits into the machine's
memory:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
123  0   -27   3492374892749289174
</PRE></BLOCKQUOTE>
<A NAME="@default18"></A> <A NAME="@default19"></A>
Floating point numbers (represented as IEEE double floats) are written
as
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
0.0 3.141592653589793 6.02e23 -35e-12 -1.0Inf
</PRE></BLOCKQUOTE>
<A NAME="@default20"></A> <A NAME="@default21"></A> <A NAME="@default22"></A> <A NAME="@default23"></A>
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> provides two additional numeric types, rationals and
bounded reals. ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> can do arithmetic with all these numeric
types.<BR>
<BR>
Note that performing arithmetic requires the use of the <A NAME="@default24"></A>
<A HREF="../bips/kernel/arithmetic/is-2.html"><B>is/2</B></A><A NAME="@default25"></A> predicate:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- X is 3 + 4.
X = 7
Yes
</PRE></BLOCKQUOTE>
<A NAME="@default26"></A>
If one just uses <A HREF="../bips/kernel/termcomp/E-2.html"><B>=/2</B></A><A NAME="@default27"></A>,
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> will simply construct a term corresponding to the
arithmetic expression, and will not evaluate it:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- X = 3 + 4.
X = 3 + 4
Yes
</PRE></BLOCKQUOTE>
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>&#8857;</B><DD CLASS="dd-description"> <FONT COLOR="#9832CC">For more details on numeric types and arithmetic in general see the
User Manual chapter on Arithmetic.</FONT>
</DL>

<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>&#8857;</B><DD CLASS="dd-description"> <FONT COLOR="#9832CC">For more information on the bounded real numeric type, see
Chapter&nbsp;</FONT><A HREF="tutorial063.html#chapreal"><FONT COLOR="#9832CC">9</FONT></A><FONT COLOR="#9832CC">.</FONT>
</DL>

<A NAME="toc11"></A>
<H3 CLASS="subsection"><A NAME="htoc23">3.1.2</A>&nbsp;&nbsp;Strings</H3>
<A NAME="@default28"></A> <A NAME="@default29"></A>
Strings are a representation for arbitrary sequences of bytes and are
written with double quotes:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
"hello"
"I am a string!"
"string with a newline \n and a null \000 character"
</PRE></BLOCKQUOTE>
Strings can be constructed and partitioned in various ways using
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> primitives.<BR>
<BR>
<A NAME="toc12"></A>
<H3 CLASS="subsection"><A NAME="htoc24">3.1.3</A>&nbsp;&nbsp;Atoms</H3>
<A NAME="@default30"></A> <A NAME="@default31"></A>
Atoms are simple symbolic constants, similar to enumeration type
constants in other languages. No special meaning is attached to them
by the language. Syntactically, all words starting with a lower case
letter are atoms, sequences of symbols are atoms, and anything in
single quotes is an atom:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
atom  quark  i486  -*-  ???  'Atom'   'an atom'
</PRE></BLOCKQUOTE>
<A NAME="toc13"></A>
<H3 CLASS="subsection"><A NAME="htoc25">3.1.4</A>&nbsp;&nbsp;Lists</H3>
<A NAME="@default32"></A> <A NAME="@default33"></A>
A list is an ordered sequence of (any number of) elements, each of
which is itself a term. Lists are delimited by square brackets (<TT>[
]</TT>), and elements are separated by a comma. Thus, the following are
lists:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[1,2,3]
[london, cardiff, edinburgh, belfast]
["hello", 23, [1,2,3], london]
</PRE></BLOCKQUOTE>
<A NAME="@default34"></A> <A NAME="@default35"></A>
A special case is the empty list (sometimes called <EM>nil</EM>), which
is written as
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[]
</PRE></BLOCKQUOTE>
A list is actually composed of head-and-tail pairs, where the head contains one
list element, and the tail is itself a list (possibly the empty list).
Lists can be written as a <TT>[Head|Tail]</TT> pair, with the head separated from
the tail by the vertical bar. Thus the list <TT>[1,2,3]</TT> can
be written in any of the following equivalent ways:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[1,2,3]
[1|[2,3]]
[1|[2|[3]]]
[1|[2|[3|[]]]]
</PRE></BLOCKQUOTE>
The last line shows that the list actually consists of 3 <TT>[Head|Tail]</TT>
pairs, where the tail of the last pair is the empty list.
The usefulness of this notation is
that the tail can be a variable (introduced below):
<TT>[1|Tail]</TT>, which leaves the tail unspecified for the moment. <BR>
<BR>
<A NAME="toc14"></A>
<H3 CLASS="subsection"><A NAME="htoc26">3.1.5</A>&nbsp;&nbsp;Structures</H3> 
<A NAME="@default36"></A> <A NAME="@default37"></A>
Structures correspond to structs or records in other languages. A
structure is an aggregate of a fixed number of components, called its
<EM>arguments</EM>. Each argument is itself a term. Moreover, a
structure always has a name (which looks like an atom). The canonical
syntax for structures is
<BLOCKQUOTE CLASS="quotation">
<I>&lt;name&gt;(&lt;arg&gt;</I><SUB><I>1</I></SUB><I>,...&lt;arg&gt;</I><SUB><I><I>n</I></I></SUB><I>)</I>
</BLOCKQUOTE>
Valid examples of structures are:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
date(december, 25, "Christmas")
element(hydrogen, composition(1,0))
flight(london, new_york, 12.05, 17.55)
</PRE></BLOCKQUOTE>
<A NAME="@default38"></A> The number of arguments of a structure is called its <EM>arity</EM>. <A NAME="@default39"></A> The name and arity of a structure are
together called its <EM>functor</EM> and is often written as <EM>name/arity</EM>. The last example above therefore has the functor <TT>flight/4</TT>.
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>&#8857;</B><DD CLASS="dd-description"> <FONT COLOR="#9832CC">See section </FONT><A HREF="tutorial024.html#structures"><FONT COLOR="#9832CC">4.1</FONT></A><FONT COLOR="#9832CC"> for information about defining structures
with named fields.</FONT>
</DL>


<H4 CLASS="subsubsection">Operator Syntax</H4> 
<A NAME="@default40"></A>
<A NAME="@default41"></A>
<A NAME="@default42"></A>
<A NAME="@default43"></A>
As a syntactic convenience, unary (1-argument) structures can also be written
in prefix or postfix notation, and binary (2-argument) structures can be
written in prefix or infix notation, if the programmer has made an
appropriate declaration (called an <EM>operator declaration</EM>)
about its functor. For example if <TT>plus/2</TT> were declared to
be an infix operator, we could write:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
1 plus 100
</PRE></BLOCKQUOTE>
instead of
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
plus(1,100)
</PRE></BLOCKQUOTE>
It is worth keeping in mind that the data term represented by the
two notations is the same, we have just two ways of writing the same thing.
Various logical and arithmetic functors are automatically declared to
allow operator syntax, for example <TT>+/2, not/1</TT> etc.<BR>
<BR>

<H4 CLASS="subsubsection">Parentheses</H4> 
When prefix, infix and postfix notation is used, it is sometimes necessary to
write extra parentheses to make clear what the structure of the written
term is meant to be. For example to write the following nested structure
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
+(*(3,4), 5)
</PRE></BLOCKQUOTE>
we can alternatively write
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
3 * 4 + 5
</PRE></BLOCKQUOTE>
because the star binds stronger than the plus sign.
But to write the following differently nested structure
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
*(3, +(4, 5))
</PRE></BLOCKQUOTE>
in infix-notation, we need extra parentheses:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
3 * (4 + 5)
</PRE></BLOCKQUOTE>
A full table of the predefined prefix, infix and postfix operators
with their relative precedences can be found in the appendix of the
User Manual.<BR>
<BR>

	<BLOCKQUOTE CLASS="figure"><DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV>
	<DIV CLASS="center">
	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#DB9370">
	
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>Numbers</B><DD CLASS="dd-description">
 ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP>has <EM>integers</EM>, <EM>floats</EM>, <EM>rationals</EM> and <EM>bounded reals</EM>.
<DT CLASS="dt-description"><B>Strings</B><DD CLASS="dd-description">
 Character sequences in double quotes.
<DT CLASS="dt-description"><B>Atoms</B><DD CLASS="dd-description">
 Symbolic constants, usually lower case or in single quotes.
<DT CLASS="dt-description"><B>Lists</B><DD CLASS="dd-description">
 Lists are constructed from cells that have an arbitrary head and
 a tail which is again a list.
<DT CLASS="dt-description"><B>Structures</B><DD CLASS="dd-description">
 Structures have a name and a certain number (<EM>arity</EM>) of arbitrary arguments.
 This characteristic is called the <EM>functor</EM>, and written name/arity.
</DL>

	</TD>
</TR></TABLE>
	</DIV>
	<BR>
<BR>
<DIV CLASS="center">Figure 3.1: Summary of Data Types</DIV><BR>
<BR>

	<DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
<HR>
<A HREF="tutorial012.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial014.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
